<?php
# Part of RAMS unittest fixtures: feed retrieval.
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class RamsPaginationTest extends UnitTestCase {
    function TestRamsClientlib() {
        $this->UnitTestCase("Rams Pagination Tests");
    }

    function testGetMiscdirPaginateBy5() {
      $entriesFound = 0;
      $first_page_all_entry = null;
      $second_page_all_entry = null;
      // try {
        # check the rootdir
        $rams = new Rams(USER, PWD, BASE);
        # get the feed
        $query = new Rams_TrafficQuery();
        $query->setYear("2009");
        $query->setMonth("all");
        $query->setPaginateBy("5");
        # get the feed
        $feed = $rams->getTrafficFeed("/miscdir", $query);
        foreach ($feed as $entry) {
          // echo "Found " . $entry->type . " entry with kind = " . $entry->kind . "\n";
          // # Retrieve the value of the params child elements
          // echo "Path: " . $entry->content->params->path->text . "\n";
          // echo "Volume: " . $entry->content->params->volume->text . "\n";
          // echo "Hits: " . $entry->content->params->hits->text . "\n";
          // echo "Updated: " . $entry->content->params->updated->text . "\n";
          switch ($entry->kind) {
            case "file":
              if (FALSE === strpos($entry->content->params->path->text, "miscfile.avi")) {
                  $entriesFound++;
                  $this->assertEqual($entry->content->params->path->text, "/miscdir");
              }
                else {
                  $entriesFound++;
                  $this->assertEqual($entry->content->params->path->text, "/miscdir/miscfile.avi");
              }
              break;
            case "dir":
              $entriesFound++;
              $this->assertEqual($entry->content->params->path->text, "/miscdir/subdir");
              break;
            case "root":
              $entriesFound++;
              $this->assertEqual($entry->content->params->path->text, "/miscdir");
              if ($entry->type == "all") {
                $first_page_all_entry = $entry;
              }
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 8); # 5 actual entries + 3 root totals (http, ftp, all)

        $entriesFound = 0; # reset
        $feed = $rams->getNextFeed($feed);
        foreach ($feed as $entry) {
          switch ($entry->kind) {
            case "dir":
              $entriesFound++;
              $this->assertEqual($entry->content->params->path->text, "/miscdir/subdir");
              break;
            case "root":
              $entriesFound++;
              $this->assertEqual($entry->content->params->path->text, "/miscdir");
              if ($entry->type == "all") {
                $second_page_all_entry = $entry;
              }
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 4); # 1 actual entry + 3 root totals (http, ftp, all)

        # check if the all entry on the first page is the same as on the secon
        $this->assertEqual($first_page_all_entry->content->params->volume->text, $second_page_all_entry->content->params->volume->text);
        $this->assertEqual($first_page_all_entry->content->params->hits->text, $second_page_all_entry->content->params->hits->text);
        
      // }
      // catch(Zend_Gdata_App_Exception $e) {
      //   $this->assertEqual("ok", $e->getResponse()->getBody());
      // }
    }
    
    function testGetSubdirFilesPaginateBy10() {
      $entriesFound = 0;
      try {
        # check the rootdir
        $rams = new Rams(USER, PWD, BASE);
        # get the feed
        $query = new Rams_TrafficQuery();
        $query->setYear("2009");
        $query->setMonth("all");
        $query->setKind("file");
        $query->setPaginateBy("10");
        # get the feed
        $feed = $rams->getTrafficFeed("/subdir", $query);
        foreach ($feed as $entry) {
          switch ($entry->content->params->path->text) {
            case "/subdir/1subfile01.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile02.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile03.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile04.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile05.png":
              $entriesFound++;
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 10); # http + ftp for every file

        $entriesFound = 0; # reset
        $feed = $rams->getNextFeed($feed);
        foreach ($feed as $entry) {
          switch ($entry->content->params->path->text) {
            case "/subdir/1subfile06.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile07.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile08.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile09.png":
              $entriesFound++;
              break;
            case "/subdir/1subfile10.png":
              $entriesFound++;
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 10); # 5 actual entries + 3 root totals (http, ftp, all)
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
    }
    
    function testRootPaginateBy5() {
      $entriesFound = 0;
      $first_page_all_entry = null;
      $second_page_all_entry = null;
      $third_page_all_entry = null;
      try {
        # check the rootdir
        $rams = new Rams(USER, PWD, BASE);
        # get the feed
        $query = new Rams_TrafficQuery();
        $query->setYear("2009");
        $query->setMonth("all");
        $query->setPaginateBy("6");
        # get the feed
        $feed = $rams->getTrafficFeed("/", $query);
        foreach ($feed as $entry) {
          switch ($entry->content->params->path->text) {
            case "/file1.txt":
              $entriesFound++;
              break;
            case "/file2":
              $entriesFound++;
              break;
            case "/miscdir":
              $entriesFound++;
              break;
            case "/":
              $entriesFound++;
              if ($entry->type == "all") {
                $first_page_all_entry = $entry;
              }
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 9); # 6 actual entries + 3 root totals (http, ftp, all)

        $entriesFound = 0; # reset
        $feed = $rams->getNextFeed($feed);
        foreach ($feed as $entry) {
          switch ($entry->content->params->path->text) {
            case "/subdir":
              $entriesFound++;
              break;
            case "/subdir2":
              $entriesFound++;
              break;
            case "/subdir3":
              $entriesFound++;
              break;
            case "/":
              $entriesFound++;
              if ($entry->type == "all") {
                $second_page_all_entry = $entry;
              }
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 9); # 6 actual entries + 3 root totals (http, ftp, all)

        $entriesFound = 0; # reset
        $feed = $rams->getNextFeed($feed);
        foreach ($feed as $entry) {
          switch ($entry->content->params->path->text) {
            case "/subdir4":
              $entriesFound++;
              break;
            case "/":
              $entriesFound++;
              if ($entry->type == "all") {
                $third_page_all_entry = $entry;
              }
              break;
            default:
              $this->assertEqual("?", $entry->content->params->name->text);
              break;
          }
        }
        $this->assertEqual($entriesFound, 5); # 2 actual entries + 3 root totals (http, ftp, all)

        # check if the all entry on the first page is the same as on the second
        $this->assertEqual($first_page_all_entry->content->params->volume->text, $second_page_all_entry->content->params->volume->text);
        $this->assertEqual($first_page_all_entry->content->params->hits->text, $second_page_all_entry->content->params->hits->text);
        # check if the all entry on the third page is the same as on the second
        $this->assertEqual($third_page_all_entry->content->params->volume->text, $second_page_all_entry->content->params->volume->text);
        $this->assertEqual($third_page_all_entry->content->params->hits->text, $second_page_all_entry->content->params->hits->text);
        
      }
      catch(Zend_Gdata_App_Exception $e) {
        $this->assertEqual("ok", $e->getResponse()->getBody());
      }
    }

}
